package com.lili.queue;

import java.util.*;

/**
 * @Auther: 李 力
 * @Date: 2024/8/21
 * @Description: 滑动窗口最大值
 * @version: 1.0
 */
public class LeetCode239 {

    public int[] maxSlidingWindow(int[] nums, int k) {
        if (nums.length == 1 || nums.length == 0) {
            return nums;
        }
        MonotonicQueue monotonicQueue = new MonotonicQueue();
        List<Integer> list = new LinkedList<>();
        for (int i = 0; i < nums.length; i++) {
            if (i >= k && monotonicQueue.peek() == nums[i - k]) {
                monotonicQueue.poll();
            }
            int num = nums[i];
            monotonicQueue.offer(num);
            if (i >= (k - 1)) {
                list.add(monotonicQueue.peek());
            }
        }
        return list.stream().mapToInt(Integer::intValue).toArray();
    }
}
